package com.ctsi.ssdc.service.impl;

import com.ctsi.ssdc.entity.EconomicIndicators;
import com.ctsi.ssdc.entity.Excel11;
import com.ctsi.ssdc.entity.FixedInvestment;
import com.ctsi.ssdc.mapper.Excel11Mapper;
import com.ctsi.ssdc.mapper.Excel12Mapper;
import com.ctsi.ssdc.service.EconomicIndicatorsService;
import com.ctsi.ssdc.service.Excel11Service;
import com.ctsi.ssdc.utils.MathUtil;
import com.ctsi.ssdc.utils.TimeUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 11基础表投资（工投基础）(Excel11)表服务实现类
 *
 * @author makejava
 * @since 2023-02-21 19:44:40
 */
@Service("excel11Service")
public class Excel11ServiceImpl implements Excel11Service {
    @Resource
    private Excel11Mapper excel11Mapper;
    @Resource
    private Excel12Mapper excel12Mapper;
    @Resource
    private EconomicIndicatorsService economicIndicatorsService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Excel11 queryById(Long id) {
        return this.excel11Mapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param excel11     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Excel11> queryByPage(Excel11 excel11, PageRequest pageRequest) {
        long total = this.excel11Mapper.count(excel11);
        return new PageImpl<>(this.excel11Mapper.queryAllByLimit(excel11, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param excel11 实例对象
     * @return 实例对象
     */
    @Override
    public Excel11 insert(Excel11 excel11) {
        this.excel11Mapper.insert(excel11);
        return excel11;
    }

    /**
     * 修改数据
     *
     * @param excel11 实例对象
     * @return 实例对象
     */
    @Override
    public Excel11 update(Excel11 excel11) {
        this.excel11Mapper.update(excel11);
        return this.queryById(excel11.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.excel11Mapper.deleteById(id) > 0;
    }

    @Override
    public boolean insertBatch(List<Excel11> list) {
        return excel11Mapper.insertBatch(list) > 0;
    }

    /**
     * 获取工业投资当月 + 累计 + 累计同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public FixedInvestment getIndustrialInvestmentInfo(String year, String month) {
        FixedInvestment info = excel11Mapper.getIndustrialInvestmentInfo(year, month);
        if (info == null) return null;
//        FixedInvestment lastYear = excel11Mapper.getIndustrialInvestmentInfo(String.valueOf(Integer.parseInt(year) - 1), month);
        // 计算同比
        EconomicIndicators lastYear = economicIndicatorsService.queryByIndicator(TimeUtil.date(String.valueOf(Integer.parseInt(year) - 1), month), "#工业投资");
        if (lastYear != null) {
            double mom = MathUtil.round((info.getAccumulate() / lastYear.getAccumulate() - 1), 2);
            info.setMonthOnMonth(mom);
        }
        if (info != null) {
            info.setStatisticalTime(TimeUtil.date(year, month));
            info.setIndicators("工业投资");
        }
        return info;
    }

    /**
     * 获取基础设施项目投资当月 + 累计 + 累计同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public FixedInvestment getBasicInvestmentInfo(String year, String month) {
        FixedInvestment info = excel11Mapper.getBasicInvestmentInfo(year, month);
        if (info == null) return null;
        // 当月
        FixedInvestment lastYear = excel11Mapper.getBasicInvestmentInfo(String.valueOf(Integer.parseInt(year) - 1), month);
        if (lastYear != null) {
            double mom = Math.round((info.getAccumulate() / lastYear.getAccumulate() - 1) * 1000) / 10;
            info.setMonthOnMonth(mom);
        }
        if (info != null) {
            info.setIndicators("基础设施项目投资");
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 全社会固定资产投资当月 + 累计 + 累计同比
     * 工业 + 房地产 + 基础设施
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public FixedInvestment getSocialFixedInvestmentInfo(String year, String month) {

        // 基础
        FixedInvestment basicInvestmentInfo = excel11Mapper.getBasicInvestmentInfo(year, month);
        FixedInvestment lastBasicInvestmentInfo = excel11Mapper.getBasicInvestmentInfo(year, month);
        // 工业
        FixedInvestment industrialInvestmentInfo = excel11Mapper.getIndustrialInvestmentInfo(year, month);
        FixedInvestment lastIndustrialInvestmentInfo = excel11Mapper.getIndustrialInvestmentInfo(year, month);
        // 房地产
        FixedInvestment realEstateInvestmentInfo = excel12Mapper.getRealEstateInvestmentInfo(year, month);
        FixedInvestment lastRealEstateInvestmentInfo = excel12Mapper.getRealEstateInvestmentInfo(year, month);
        if (basicInvestmentInfo ==null || industrialInvestmentInfo==null || realEstateInvestmentInfo == null) return null;
        FixedInvestment info = new FixedInvestment();
        info.setStatisticalTime(TimeUtil.date(year, month));
        info.setIndicators("全社会固定资产投资");
        info.setThisMonth(MathUtil.round(basicInvestmentInfo.getThisMonth() + industrialInvestmentInfo.getThisMonth() + realEstateInvestmentInfo.getThisMonth(),2));
        info.setAccumulate(MathUtil.round(basicInvestmentInfo.getAccumulate() + industrialInvestmentInfo.getAccumulate() + realEstateInvestmentInfo.getAccumulate(),2));
        EconomicIndicators query = economicIndicatorsService.queryByIndicator(TimeUtil.date(Integer.parseInt(year) - 1, Integer.parseInt(month)), "全社会固定资产投资");
        if (query != null) {
            // 当e表存在去年数据时 ->
            info.setMonthOnMonth((MathUtil.round((info.getAccumulate() / query.getAccumulate() - 1) * 100, 1)));
        } else if (lastBasicInvestmentInfo.getAccumulate() != null &&
                lastIndustrialInvestmentInfo.getAccumulate() != null &&
                lastRealEstateInvestmentInfo.getAccumulate() != null) {
            // 去年累计数据 用于计算同比
            Double lastAccumulate = lastBasicInvestmentInfo.getAccumulate() + lastIndustrialInvestmentInfo.getAccumulate() + lastRealEstateInvestmentInfo.getAccumulate();
            info.setMonthOnMonth((MathUtil.round((info.getAccumulate() / lastAccumulate - 1) * 100, 1)));
        }
        return info;
    }

    /**
     * 根据日期删除
     *
     * @param date
     * @return 是否成功
     */
    @Override
    public boolean removeByDate(Date date) {
        return excel11Mapper.removeByDate(date);
    }
}
